home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / fractals / mandelsquare / mandelsquare-1.06.lha / PlayAnim.c < prev    next >
C/C++ Source or Header  |  1992-10-31  |  16KB  |  772 lines

  1. /*
  2. **    MandelSquare - AmigaDOS 2.0/3.0 Mandelbrot set explorer
  3. **
  4. **    PlayAnim.c, Routines to replay IFF-ANIM files
  5. **
  6. **    Copyright © 1991-1992 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10.     /* Animation module error codes, as returned by ValidateAnim(). */
  11.  
  12. enum    {        ANIMERR_WRONG_SEQUENCE=1,ANIMERR_NO_BODY,ANIMERR_WRONG_OPERATION,ANIMERR_NO_ANIM,
  13.             ANIMERR_MASKING,ANIMERR_IMAGE,ANIMERR_COMPRESSION,ANIMERR_NO_HEADER,ANIMERR_WRONG_SIZE,
  14.             ANIMERR_WRONG_MODE };
  15.  
  16.     /* Animation file list entry. */
  17.  
  18. struct AnimNode
  19. {
  20.     struct MinNode     Node;
  21.  
  22.     BitMapHeader    *BitMapHeader;
  23.     AnimationHeader    *AnimationHeader;
  24.  
  25.     APTR         Body;
  26.  
  27.     ULONG        *ViewModes,
  28.              Modes;
  29.  
  30.     struct Palette    *Palette;
  31. };
  32.  
  33.     /* Global flag. */
  34.  
  35. extern BYTE        Is39;
  36.  
  37.     /* In Double.c */
  38.  
  39. extern VOID        FreeDBI(struct DBI *Info);
  40. extern struct DBI *    AllocDBI(struct Screen *Screen);
  41. extern VOID        SwapDBI(struct DBI *Info,BYTE Copy);
  42. extern struct BitMap *    GetDBI(struct DBI *Info);
  43. extern VOID        PaletteDBI(struct DBI *Info,struct Palette *Palette);
  44. extern VOID        StartDBI(struct DBI *Info,ULONG Jiffies);
  45. extern VOID        WaitDBI(struct DBI *Info);
  46.  
  47.     /* In Palette.c */
  48.  
  49. extern VOID        FreePalette(struct Palette *Palette);
  50. extern struct Palette *    AllocPalette(LONG NumColours,BYTE TrueColour);
  51. extern struct Palette *    GetPalette(struct Screen *Screen,struct Palette *Palette);
  52. extern VOID        LoadPalette(struct Screen *Screen,struct Palette *Palette,LONG NumColours);
  53. extern LONG        GetPaletteSize(struct Palette *Palette);
  54. extern BYTE        GetPaletteTriplet(struct Palette *Palette,UBYTE *Triplet,LONG Index);
  55. extern BYTE        SetPaletteTriplet(struct Palette *Palette,UBYTE R,UBYTE G,UBYTE B,LONG Index);
  56. extern ULONG        GetPaletteEntry(struct Palette *Palette,LONG Index);
  57. extern BYTE        SetPaletteEntry(struct Palette *Palette,ULONG Entry,LONG Index);
  58.  
  59.     /* In unvscomp.asm */
  60.  
  61. extern VOID __asm    decode_vkplane(register __a0 UBYTE *deltabyte,register __a2 PLANEPTR plane,register __a3 UWORD *ytable,register __d2 UWORD bytesperrow);
  62.  
  63.     /* Local, static routines. */
  64.  
  65. STATIC __inline VOID    UnPackRow(BYTE **SourcePtr,BYTE **DestinationPtr,register WORD DestinationBytes);
  66. STATIC __inline VOID    SetRIFF(struct BitMap *BitMap,struct AnimNode *Node,UWORD *LineTable);
  67. STATIC __inline VOID    SetBODY(struct BitMap *BitMap,struct AnimNode *Node);
  68.  
  69. STATIC BYTE __regargs    GetBitMapHeader(struct AnimNode *Node,BitMapHeader *Header);
  70. STATIC BYTE __regargs    GetAnimationHeader(struct AnimNode *Node,AnimationHeader *Header);
  71. STATIC BYTE __regargs    GetColourMap(struct AnimNode *Node,UBYTE *Palette,LONG Size);
  72. STATIC VOID __regargs    DeleteAnimNode(struct AnimNode *Node);
  73.  
  74.     /* Exported routines. */
  75.  
  76. VOID            DeleteAnim(struct MinList *List);
  77. struct MinList *    LoadAnim(STRPTR Name);
  78. LONG            ValidateAnim(struct MinList *List,struct Screen *Screen);
  79. BYTE            PlayAnim(struct MinList *List,struct Screen *Screen,BYTE (*CheckAbort)(VOID));
  80.  
  81.  
  82.     /* UnPackRow(BYTE **SourcePtr,BYTE **DestinationPtr,register WORD DestinationBytes):
  83.      *
  84.      *    CmpByteRun unpacker, based on original EA IFF code.
  85.      */
  86.  
  87. STATIC __inline VOID
  88. UnPackRow(BYTE **SourcePtr,BYTE **DestinationPtr,register WORD DestinationBytes)
  89. {
  90.     register BYTE    *Source        = *SourcePtr,
  91.             *Destination    = *DestinationPtr,
  92.             Char;
  93.     register WORD    Count;
  94.  
  95.     while(DestinationBytes > 0)
  96.     {
  97.         if((Count = *Source++) >= 0)
  98.         {
  99.             Count++;
  100.  
  101.             if((DestinationBytes -= Count) < 0)
  102.                 break;
  103.             else
  104.             {
  105.                 do
  106.                     *Destination++ = *Source++;
  107.                 while(--Count);
  108.             }
  109.         }
  110.         else
  111.         {
  112.             if(Count != (WORD)(-128))
  113.             {
  114.                 Count = -Count + 1;
  115.  
  116.                 if((DestinationBytes -= Count) < 0)
  117.                     break;
  118.                 else
  119.                 {
  120.                     Char = *Source++;
  121.  
  122.                     do
  123.                         *Destination++ = Char;
  124.                     while(--Count);
  125.                 }
  126.             }
  127.         }
  128.     }
  129.  
  130.     *SourcePtr        = Source;
  131.     *DestinationPtr        = Destination;
  132. }
  133.  
  134.     /* SetRIFF(struct BitMap *BitMap,struct AnimNode *Node,UWORD *LineTable):
  135.      *
  136.      *    Unpack RIFF style delta data.
  137.      */
  138.  
  139. STATIC __inline VOID
  140. SetRIFF(struct BitMap *BitMap,struct AnimNode *Node,UWORD *LineTable)
  141. {
  142.     register LONG    *DeltaData = (LONG *)Node -> Body,
  143.              i;
  144.  
  145.         /* Run down the planes... */
  146.  
  147.     for(i = 0 ; i < BitMap -> Depth ; i++)
  148.     {
  149.             /* Any data to process? If so, unpack it. */
  150.  
  151.         if(DeltaData[i])
  152.             decode_vkplane((UBYTE *)DeltaData + DeltaData[i],BitMap -> Planes[i],LineTable,BitMap -> BytesPerRow);
  153.     }
  154. }
  155.  
  156.     /* SetBODY(struct BitMap *BitMap,struct AnimNode *Node):
  157.      *
  158.      *    Unpack CmpByteRun data.
  159.      */
  160.  
  161. STATIC __inline VOID
  162. SetBODY(struct BitMap *BitMap,struct AnimNode *Node)
  163. {
  164.     PLANEPTR     Planes[8],
  165.             *Destination;
  166.     BYTE        *Source;
  167.     LONG         i,j;
  168.  
  169.         /* Copy the plane addresses, we will modify the pointers. */
  170.  
  171.     for(i = 0 ; i < BitMap -> Depth ; i++)
  172.         Planes[i] = BitMap -> Planes[i];
  173.  
  174.         /* Copy the data pointer, this will be modified as well. */
  175.  
  176.     Source = (BYTE *)Node -> Body;
  177.  
  178.         /* Run down the rows... */
  179.  
  180.     for(i = 0 ; i < BitMap -> Rows ; i++)
  181.     {
  182.             /* Run down the planes... */
  183.  
  184.         for(j = 0 ; j < BitMap -> Depth ; j++)
  185.         {
  186.                 /* Remember the address. */
  187.  
  188.             Destination = &Planes[j];
  189.  
  190.                 /* Unpack the data. */
  191.  
  192.             UnPackRow(&Source,Destination,BitMap -> BytesPerRow);
  193.         }
  194.     }
  195. }
  196.  
  197.     /* GetBitMapHeader(struct AnimNode *Node,BitMapHeader *Header):
  198.      *
  199.      *    Attach a bitmap header to an animnode.
  200.      */
  201.  
  202. STATIC BYTE __regargs
  203. GetBitMapHeader(struct AnimNode *Node,BitMapHeader *Header)
  204. {
  205.     if(Node -> BitMapHeader = (BitMapHeader *)AllocVec(sizeof(BitMapHeader),MEMF_ANY))
  206.     {
  207.         CopyMem(Header,Node -> BitMapHeader,sizeof(BitMapHeader));
  208.  
  209.         return(TRUE);
  210.     }
  211.     else
  212.         return(FALSE);
  213. }
  214.  
  215.     /* GetAnimationHeader(struct AnimNode *Node,AnimationHeader *Header):
  216.      *
  217.      *    Attach and animation header to an animnode.
  218.      */
  219.  
  220. STATIC BYTE __regargs
  221. GetAnimationHeader(struct AnimNode *Node,AnimationHeader *Header)
  222. {
  223.     if(Node -> AnimationHeader = (AnimationHeader *)AllocVec(sizeof(AnimationHeader),MEMF_ANY))
  224.     {
  225.         CopyMem(Header,Node -> AnimationHeader,sizeof(AnimationHeader));
  226.  
  227.         return(TRUE);
  228.     }
  229.     else
  230.         return(FALSE);
  231. }
  232.  
  233.     /* GetColourMap(struct AnimNode *Node,UBYTE *Palette,LONG Size):
  234.      *
  235.      *    Attach a colour map to an animnode.
  236.      */
  237.  
  238. STATIC BYTE __regargs
  239. GetColourMap(struct AnimNode *Node,UBYTE *Palette,LONG Size)
  240. {
  241.         /* Determine number of palette entries. */
  242.  
  243.     LONG Count = Size / 3;
  244.  
  245.         /* Allocate a palette. */
  246.  
  247.     if(Node -> Palette = AllocPalette(Count,Count == 256))
  248.     {
  249.         LONG    R,G,B,
  250.             i;
  251.  
  252.             /* Eight bits? */
  253.  
  254.         if(Count == 256)
  255.         {
  256.                 /* Run down the table, setting the eight bit triplets. */
  257.  
  258.             for(i = 0 ; i < Count ; i++)
  259.             {
  260.                 R = *Palette++;
  261.                 G = *Palette++;
  262.                 B = *Palette++;
  263.  
  264.                 SetPaletteTriplet(Node -> Palette,R,G,B,i);
  265.             }
  266.         }
  267.         else
  268.         {
  269.                 /* Run down the table, setting the four bit triplets. */
  270.  
  271.             for(i = 0 ; i < Count ; i++)
  272.             {
  273.                 R = ((*Palette++) >> 4) & 0xF;
  274.                 G = ((*Palette++) >> 4) & 0xF;
  275.                 B = ((*Palette++) >> 4) & 0xF;
  276.  
  277.                 SetPaletteTriplet(Node -> Palette,R,G,B,i);
  278.             }
  279.         }
  280.  
  281.         return(TRUE);
  282.     }
  283.     else
  284.         return(FALSE);
  285. }
  286.     /* DeleteAnimNode(struct AnimNode *Node):
  287.      *
  288.      *    Free an animation list node.
  289.      */
  290.  
  291. STATIC VOID __regargs
  292. DeleteAnimNode(struct AnimNode *Node)
  293. {
  294.     if(Node -> BitMapHeader)
  295.         FreeVec(Node -> BitMapHeader);
  296.  
  297.     if(Node -> AnimationHeader)
  298.         FreeVec(Node -> AnimationHeader);
  299.  
  300.     if(Node -> Body)
  301.         FreeVec(Node -> Body);
  302.  
  303.     if(Node -> Palette)
  304.         FreePalette(Node -> Palette);
  305.  
  306.     FreeVec(Node);
  307. }
  308.  
  309.     /* DeleteAnim(struct MinList *List):
  310.      *
  311.      *    Delete the contents of an entire animation list.
  312.      */
  313.  
  314. VOID
  315. DeleteAnim(struct MinList *List)
  316. {
  317.     struct AnimNode    *Node,
  318.             *Next;
  319.  
  320.         /* Pick up the first node in the list. */
  321.  
  322.     Node = (struct AnimNode *)List -> mlh_Head;
  323.  
  324.         /* Run down the list... */
  325.  
  326.     while(Next = (struct AnimNode *)Node -> Node . mln_Succ)
  327.     {
  328.             /* Free the node. */
  329.  
  330.         DeleteAnimNode(Node);
  331.  
  332.             /* Skip to the next. */
  333.  
  334.         Node = Next;
  335.     }
  336.  
  337.         /* Remove the list itself. */
  338.  
  339.     FreeVec(List);
  340. }
  341.  
  342.     /* LoadAnim(STRPTR Name):
  343.      *
  344.      *    Load an ANIM5 animation file, creating a list suitable for
  345.      *    submitting to PlayAnim() or ValidateAnim().
  346.      */
  347.  
  348. struct MinList *
  349. LoadAnim(STRPTR Name)
  350. {
  351.         /* Data we will pick up on the fly. */
  352.  
  353.     STATIC ULONG Properties[2 * 4] =
  354.     {
  355.         ID_ILBM,ID_BMHD,
  356.         ID_ILBM,ID_ANHD,
  357.         ID_ILBM,ID_CAMG,
  358.         ID_ILBM,ID_CMAP
  359.     };
  360.  
  361.         /* Image and delta data. */
  362.  
  363.     STATIC ULONG Stops[2 * 2] =
  364.     {
  365.         ID_ILBM,ID_BODY,
  366.         ID_ILBM,ID_DLTA
  367.     };
  368.  
  369.     struct MinList *L